package com.itfreer.special.index;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.springframework.stereotype.Component;
import com.itfreer.form.api.JdbcBaseDaoImp;

/**
 * 定义指标 jdbc实现类
 */
@Component
public class JdbcIndexDataDao extends JdbcBaseDaoImp<IndexDataEntity> implements IndexDataDao {
	public JdbcIndexDataDao(EntityManager entityManager) {
		super(IndexDataEntity.class, entityManager);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getIndexDataBy(String indexid, String adminid, Integer year, String indexname) {
		// 自定义查询
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			String sql = "SELECT id as id, s_admin_id as adminid, s_index_id as indexid,"
					+ " s_index_name as indexname, i_month as month, s_unit as unit,"
					+ " d_value as value, i_year as year FROM special_index_data WHERE ";
			if (indexid != null && indexid.length() > 0) {
				sql = sql + "s_index_id = '" + indexid + "'";
			} else {
				return null;
			}

			if (adminid != null && adminid.length() > 0) {
				sql = sql + " AND s_admin_id in (SELECT id from sso_organize_administrative WHERE id = '" + adminid
						+ "'" + " OR pid = '" + adminid + "'" + ") ";
			}

			if (year != null) {
				sql = sql + "AND i_year = '" + year + "'";
			}

			if (indexname != null) {
				sql = sql + "AND s_index_name = '" + indexname + "'";
			}
			sql = sql + " ORDER BY i_year";
			Query query = em.createNativeQuery(sql);
//			query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
			list = query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;

	}

	@SuppressWarnings("unchecked")
	public Map<String, Object> getadminById(String id) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			String sql = "SELECT s_display as adminname FROM sso_organize_administrative WHERE s_code = '" + id + "'";
			Query query = em.createNativeQuery(sql);
//			query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
			list = query.getResultList();
		} catch (Exception var5) {
			throw var5;
		}
		return list.get(0);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getIndexName(String indexid) {
		// 自定义查询
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			String sql = "SELECT s_index_name as indexname FROM special_index_data WHERE ";
			sql = sql + "s_index_id = '" + indexid + "'";
			sql = sql + " GROUP BY s_index_name";
			Query query = em.createNativeQuery(sql);
//			query.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
			list = query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
